package com.ssyc.gsk_parents.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.base.SpKeys;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BannerManager;
import com.ssyc.common.quanzi.MyQuanziActivity;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.activity.ParentEventActivity;
import com.ssyc.gsk_parents.activity.ParentNoticeActivity;
import com.ssyc.gsk_parents.bean.CircleInfo;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 家长端圈子Fragment
 * Created by Administrator on 2018/6/12 0012.
 */

public class ParentQuanZiFragment extends LazyBaseFragment implements View.OnClickListener {

    private LinearLayout llContent;
    private RelativeLayout rlLodingView;
    private Banner banner;
    private LinearLayout llGg;
    private LinearLayout llHd;
    private LinearLayout llQz;

    private ImageView ivNoticeFlag;//公告的标记
    private TextView tvNotice;//公告的内容

    private ImageView ivMettingFlag;//活动的标记
    private TextView tvMetting;//活动的内容

    private ImageView ivCircleFlag;//活动的标记
    private TextView tvCircle;//活动的内容

    //圈子的数据模型
    private CircleInfo.DataBean data;

    public static final String TAG = ParentQuanZiFragment.class.getName();

    //单体模式
    private static class LazyHolder {
        private static final ParentQuanZiFragment INSTANCE = new ParentQuanZiFragment();
    }

    public static final ParentQuanZiFragment newInstance() {
        return LazyHolder.INSTANCE;
    }


    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.parent_fragment_quanzi;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }


    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);
        showContent();

        //初始化视图
        initView(view);

        //请求服务器
        http(true);
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view) {
        rlLodingView = view.findViewById(R.id.rl_loading);
        llContent = view.findViewById(R.id.ll_content);
        rlLodingView.setVisibility(View.VISIBLE);
        llContent.setVisibility(View.GONE);
        banner = view.findViewById(R.id.banner);
        llGg = view.findViewById(R.id.ll_gg);
        llGg.setOnClickListener(this);
        llHd = view.findViewById(R.id.ll_hd);
        llHd.setOnClickListener(this);
        llQz = view.findViewById(R.id.ll_qz);
        llQz.setOnClickListener(this);

        ivNoticeFlag = view.findViewById(R.id.iv_notice_flag);
        tvNotice = view.findViewById(R.id.tv_notice);

        ivMettingFlag = view.findViewById(R.id.iv_metting_flag);
        tvMetting = view.findViewById(R.id.tv_metting);

        ivCircleFlag = view.findViewById(R.id.iv_circle_flag);
        tvCircle = view.findViewById(R.id.tv_circle);

//        DisplayMetrics dm = getResources().getDisplayMetrics();
//        int width = dm.widthPixels;
//        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) banner.getLayoutParams();
//        params.height=width/2;//设置当前控件布局的高度
//        banner.setLayoutParams(params);
    }

    /**
     * 请求服务器获取页面数据
     */
    private void http(boolean flag) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "12");
        map.put("platform", "8");
        map.put("acc", AccountUtils.getAccount(getActivity()));
        map.put("role", AccountUtils.getRole(getActivity()));
        map.put("apptoken", AccountUtils.getToken(getActivity()));

        //省市区
        map.put("province", SPUtil.getString(getContext(), SpKeys.PROVINCE));
        map.put("city", SPUtil.getString(getContext(), SpKeys.CITY));
        map.put("area", SPUtil.getString(getContext(), SpKeys.AREA));

        //上传id
        map.put("NoticeInfo_ID", SPUtil.getString(getContext(), SpKeys.NOTICE));
        map.put("MeetingInfo_ID", SPUtil.getString(getContext(), SpKeys.MEETING));
        map.put("CircleMessage_ID", SPUtil.getString(getContext(), SpKeys.CIRCLE));

        HttpUtils.post(HttpAdress.CIRCLE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLodingView.getVisibility() != View.GONE) {
                    rlLodingView.setVisibility(View.GONE);
                }

                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (!TextUtils.isEmpty(response)) {
                    CircleInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, CircleInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }

                    Log.i("test", response);

                    if (null != info) {
                        //获取轮播图的数据
                        data = info.getData();
                        if (null != data) {
                            List<String> bannerUrls = data.RollPicture;
                            //设置轮播图
                            BannerManager.setData(banner, flag, bannerUrls, Transformer.Default);

                            if (rlLodingView.getVisibility() != View.GONE) {
                                rlLodingView.setVisibility(View.GONE);
                            }
                            if (llContent.getVisibility() != View.VISIBLE) {
                                llContent.setVisibility(View.VISIBLE);
                            }

                            //设置其他数据
                            //设置公告
                            if ("200".equals(data.NoticeInfo_state)) {
                                //设置标记的显示
                                if ("1".equals(data.NoticeInfo_isNew)) {
                                    //表示存在公告信息
                                    tvNotice.setText(data.NoticeInfo_Content);
                                    //标识有新消息
                                    ivNoticeFlag.setVisibility(View.VISIBLE);
                                } else {
                                    ivNoticeFlag.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                ivNoticeFlag.setVisibility(View.INVISIBLE);
                                tvNotice.setText("我的公告");
                            }

                            //设置活动
                            if ("200".equals(data.MeetingInfo_state)) {
                                //设置标记的显示
                                if ("1".equals(data.MeetingInfo_isNew)) {
                                    //标识有新消息
                                    ivMettingFlag.setVisibility(View.VISIBLE);
                                    //表示存在公告信息
                                    tvMetting.setText(data.MeetingInfo_Content);

                                } else {
                                    ivMettingFlag.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                ivMettingFlag.setVisibility(View.INVISIBLE);
                                tvMetting.setText("我的活动");
                            }

                            //设置圈子
                            if ("200".equals(data.CircleInfo_state)) {

                                //设置标记的显示
                                if ("1".equals(data.CircleInfo_isNew)) {
                                    //表示存在公告信息
                                    tvCircle.setText(data.CircleInfo_Content);

                                    //标识有新消息
                                    ivCircleFlag.setVisibility(View.VISIBLE);
                                } else {
                                    ivCircleFlag.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                ivCircleFlag.setVisibility(View.INVISIBLE);
                                tvCircle.setText("我的圈子");
                            }
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        //当页面可见的时候重新请求服务器同步状态
        if (!hidden) {
            http(false);
        }
    }


    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ll_gg) {
            //公告
            if (data != null) {
                //点击后保存公告id
                if (data.NoticeInfo_ID != 0) {
                    SPUtil.put(getContext(), SpKeys.NOTICE, data.NoticeInfo_ID + "");
                } else {
                    SPUtil.put(getContext(), SpKeys.NOTICE, "");
                }
            }

            UiUtils.startActivity(getContext(), ParentNoticeActivity.class);
        } else if (id == R.id.ll_hd) {
            if (data != null) {
                //点击后保存活动id
                if (data.MeetingInfo_ID != 0) {
                    SPUtil.put(getContext(), SpKeys.MEETING, data.MeetingInfo_ID + "");
                } else {
                    SPUtil.put(getContext(), SpKeys.MEETING, "");
                }
            }
            //活动
            UiUtils.startActivity(getContext(), ParentEventActivity.class);
        } else if (id == R.id.ll_qz) {
            if (data != null) {
                //点击后保存活动id
                if (data.CircleInfo_ID != 0) {
                    SPUtil.put(getContext(), SpKeys.CIRCLE, data.CircleInfo_ID + "");
                } else {
                    SPUtil.put(getContext(), SpKeys.CIRCLE, "");
                }
            }
            //圈子
            UiUtils.startActivity(getContext(), MyQuanziActivity.class);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        if (null != banner) {
            banner.startAutoPlay();
        }

    }

    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        if (null != banner) {
            banner.stopAutoPlay();
        }
    }
}
